﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataFileApp
{
    public partial class FormAdd : Form
    {
        bool isTreeAddvalue = false;
        string path;

        
        public FormAdd(string path, bool isTreeAddvalue = false)
        {
            InitializeComponent();
            this.isTreeAddvalue = isTreeAddvalue;
            if (isTreeAddvalue)
            {
                cType.Enabled = false;
                tValue.Enabled = false;
                groupBoxComments.Enabled = false;
            }
            this.path = path;
        }
        public event Action<string, string, string,JObject> sendData;
        private void button1_Click(object sender, EventArgs e)
        {
            List<string> t = path.Split('.').ToList<string>();
            JObject jo = new JObject();
            string key;
            string key_coments;
            if (!isTreeAddvalue)  //dataGridView添加数据
            {
                t.Add(tKey.Text);
                key = string.Join(".", t);
                key_coments = string.Join("_", t);
                if (string.IsNullOrEmpty(cType.Text) || string.IsNullOrEmpty(tKey.Text) || string.IsNullOrEmpty(tValue.Text))
                {
                    MessageBox.Show("key,value,type都需要填写");
                    return;
                }

                if (!jo.ContainsKey(key_coments))
                {
                    JProperty jp1 = new JProperty("zh", comments_zh.Text);
                    JProperty jp2 = new JProperty("en", comments_en.Text);
                    JProperty jp3 = new JProperty("de", comments_de.Text);

                    jo.Add( jp1);
                    jo.Add(jp2);
                    jo.Add(jp3);
                }


            }
            else    //TreeView添加数据
            {
             
                t.Add(tKey.Text);
                key = string.Join(".", t);
                if (string.IsNullOrEmpty(tKey.Text))
                {
                    MessageBox.Show("key需要填写");
                    return;
                }
            }

            switch (cType.Text)
            {
                case "Integer":
                    if (!int.TryParse(tValue.Text, out int ires))
                    {
                        MessageBox.Show("请输入正确的整数");
                        return;
                    }
                    break;
                case "Float":
                    if (!float.TryParse(tValue.Text, out float fres))
                    {
                        MessageBox.Show("请输入正确的浮点数");
                        return;
                    }
                    break;
                case "String":
                   
                    break;
                case "Boolean":
                    if (!bool.TryParse(tValue.Text, out bool bres))
                    {
                        MessageBox.Show("请输入正确的浮点数");
                        return;
                    }
                    break;
            }

            sendData(key, cType.Text, tValue.Text,jo);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
        }

        private void cType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cType.Text == "Boolean")
            {
                tValue.DropDownStyle = ComboBoxStyle.DropDownList;
                tValue.Items.Add("True");
                tValue.Items.Add("False");
            }
            else
            {
                tValue.DropDownStyle = ComboBoxStyle.DropDown;
                tValue.Items.Clear();
                tValue.Text = "";
            }
        }

        private void Add_Load(object sender, EventArgs e)
        {

        }
    }
}
